﻿using Microsoft.Practices.Prism.MefExtensions;
using Microsoft.Practices.Prism.Regions;
using Neudesic.Pulse.Infrastructure;
using System.ComponentModel.Composition.Hosting;
using System.Windows;

namespace Neudesic.Pulse.Notifier
{
    /// <summary>
    /// Bootstrapper responsible for setting up container and dependencies.
    /// </summary>
    public class Bootstrapper : MefBootstrapper
    {

        /// <summary>
        /// Configures the aggregate catalog.
        /// </summary>
        protected override void ConfigureAggregateCatalog()
        {
            // Add catalogs to find parts for composition.
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(this.GetType().Assembly));
        }

        /// <summary>
        /// Initializes the shell.
        /// </summary>
        protected override void InitializeShell()
        {
            base.InitializeShell();
            // Set the main window to the shell object and show it.
            Application.Current.MainWindow = (Shell)this.Shell;
            Application.Current.MainWindow.Show();
        }

        /// <summary>
        /// Creates the shell.
        /// </summary>
        /// <returns></returns>
        protected override DependencyObject CreateShell()
        {
            // return a new shell object
            return this.Container.GetExportedValue<Shell>();
        }

        protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
        {
            var factory = base.ConfigureDefaultRegionBehaviors();
            // Instead of relying on IModule behavior to define correlation of regions to views, we attach a behavior that does this for us using MEF.
            factory.AddIfMissing("AutoPopulateExportedViewsBehavior", typeof(AutoPopulateExportedViewsBehavior));
            return factory;
        }
    }
}
